package colm.lagou.city.service;

import colm.lagou.city.dao.CityDao;
import colm.lagou.city.pojo.City;
import com.lagou.common.entity.IdWorker;
import com.lagou.common.entity.StatusCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class CityService {
    @Autowired
    private CityDao cityDao;

    @Autowired
    private IdWorker idWorker;

    /**
     * 查询所有
     */
    public List<City> findAll(){
        return cityDao.findAll();
    }

    /**
     * 根据id查询
     */
    @Cacheable(value = "city",key = "#city.id")  //缓存这个方法的返回值
    public City findById(String id){
        return cityDao.findById(id).get();
    }

    /**
     * 增加
     */
    public void add(City city){
        city.setId(idWorker.nextId()+"");
        cityDao.save(city);
    }

    /**
     * 修改
     */
    public void update(City city){
        cityDao.save(city);
    }

    /**
     * 删除
     */
    @CacheEvict(value = "city",key = "#id")    //清空或者移出某个缓存
    public void delete(String id){
        cityDao.deleteById(id);
    }

    /**
     * 查询可用城市的前三条
     */
    public List<City> findTop3AndStateIsTrue(){
        return cityDao.findTop3ByState("1");
    }

    /**
     * state  状态0禁用 1启用 2热门
     * 把城市从禁用改为启用状态
     */
    public void updateCityEnable(String id){
        cityDao.updateCityEnable(id);
    }
}
